#include "app_Common.h"
#include "string.h"
#include "stdio.h"

long App_Common_get_Currenttime(void)
{
    struct timeval val;
    gettimeofday(&val,NULL);
    return val.tv_sec * 1000 + val.tv_usec / 1000 ;
}
/**
 * @brief 将16进制字符串转化为字符数组
 * 
 * @param hexstr 16进制字符串
 * @param bytesbuf 字符数组
 */
int App_Common_hexstr_to_bytes(char *hexstr, char *bytesbuf)
{
    size_t i;
    for (i = 0; i < strlen(hexstr); i+=2)
    {
        sscanf(hexstr + i,"%2hhx",bytesbuf + i / 2);
    }
    return strlen(hexstr) / 2;

    
}

/**
 * @brief 将字符数组转化为16进制字符串
 * 
 * @param bytesbuf 字符数组
 * @param hexstr 16进制字符串
 */
void App_Common_bytes_to_hexstr(char *bytesbuf, char *hexstr,int datalen)
{
    
    for (int i = 0; i < datalen; i++)
    {
        sprintf(hexstr + 2 * i,"%02x",bytesbuf[i]);
    }
    hexstr[2 * datalen] = '\0';
}

/**
 * @brief 打印字符数组
 * 
 * @param bytesbuf 字符数组
 * @param datalen 数组长度
 */
void App_Common_Printf_Bytes(char *bytesbuf, int datalen)
{
    for (int i = 0; i < datalen; i++)
    {
        printf(" %02x ",bytesbuf[i]);
    }
    printf("\n");
}
